﻿using System;
using System.IO;
using System.Net;
using System.Text;

using Kannon.Core;

namespace Kannon.Middleware
{
    public class ErrorHandlerMiddleware : KannonMiddleware
    {
        public override KannonResponse Handle(KannonRequest request)
        {
            try
            {
                var response = Application.Handle(request);
                return response;
            } // try
            catch(Exception e)
            {
                var message = Encoding.UTF8.GetBytes(e.ToString());
                var messageStream = new MemoryStream(message);
                return new KannonResponse(HttpStatusCode.InternalServerError, e.GetType().FullName + " at " + e.Source, null, messageStream);
            } // catch
        }
    }
}
